const goodsModel = require('../model/goods/GoodsModel')
const CatesModel = require('../model/goods/CatesModel')

class GoodsController {
  /**
   * 处理相关的订单的列表
   * 逻辑应该是：点击哪个商品类型，就根据商品类型筛选相应的商品
  */
  static async list (ctx) {
    let { size, cate } = ctx.request.query
    size = parseInt(size) || 10
    cate = cate || ''
    const params = {
      cate
    }
    if (!cate) delete params.cate
    
    const total = await goodsModel.find().count()
    const list = await goodsModel.find(params).limit(size)
    ctx.body = { err: 0, msg: 'success', data: { list, total } }
  }

  // 获取商品类型
  static async cates (ctx) {
    const list = await CatesModel.find().then()
    ctx.body = { err: 0, msg: 'success', data: { list } }
  }

  // 获取商品的详情信息
  static async details (ctx) {
    let { id } = ctx.request.query
    const list = await goodsModel.findById(id)
    ctx.body = { err: 0, msg: 'success', data: { list } }
  }
}

module.exports = GoodsController